package com.ie.service.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.entity.GoodsTypeEntity;
import com.ie.mapper.admin.GoodsTypeMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author S2
 */
@Service
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeMapper, GoodsTypeEntity> implements GoodsTypeService {
    @Override
    public ResponseResult<Map<String, Object>> getGoodsType(GoodsTypeEntity goodsTypeEntity) {
       //为了apipost调试用，currentPage为空时，默认为1
        Integer currentPage = goodsTypeEntity.getCurrentPage();
        if (currentPage == null) {
            goodsTypeEntity.setCurrentPage(1);
        }// 保证apipostdebug时，currentPage为空时，默认为1
        IPage<GoodsTypeEntity> iPage = new Page<>(goodsTypeEntity.getCurrentPage(), 5);
        IPage<GoodsTypeEntity> page = page(iPage);
        Map<String, Object> myres = new HashMap<>();
        myres.put("allGoodsTypes", page.getRecords());
        myres.put("totalPage", page.getPages());
        return ResponseResult.getSuccessResult(myres);

    }

    @Override
    public ResponseResult<Map<String, Object>> update(GoodsTypeEntity goodsTypeEntity) {
        boolean res = updateById(goodsTypeEntity);
        if (res) {
            return ResponseResult.getMessageResult(null,"A001");
        } else {
            return ResponseResult.getMessageResult(null,"A002");
        }
    }

    @Override
    public ResponseResult<Map<String, Object>> delete(GoodsTypeEntity googsTypeEntity) {
        //首先应该查询关联
        //TODO
        //订单,购物车有相关商品不能删除
        long res=0;//商品类型可删除
        if (res==0){
            boolean res1 = removeById(googsTypeEntity);
            if (res1) {
                return ResponseResult.getMessageResult(null,"A001");
            } else {
                return ResponseResult.getMessageResult(null,"A002");
            }
        }
       return ResponseResult.getMessageResult(null,"A003");
    }

    @Override
    public ResponseResult<Map<String, Object>> addGoodsType(GoodsTypeEntity goodsTypeEntity) {
        boolean res = save(goodsTypeEntity);
        if (res) {
            return ResponseResult.getMessageResult(null,"A001");
        } else {
            return ResponseResult.getMessageResult(null,"A002");
        }
    }
}
